package com.uobgroup.ews.uam.web;

import com.uobgroup.ews.uam.core.domain.ApplicationConfiguration;
import com.uobgroup.ews.uam.core.service.ApplicationConfigurationService;
import java.io.UnsupportedEncodingException;
import java.security.Principal;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.UriUtils;
import org.springframework.web.util.WebUtils;

@RequestMapping("/applicationconfigurations")
@Controller
public class ApplicationConfigurationController {

   @Autowired
   ApplicationConfigurationService applicationConfigurationService;

   @RequestMapping(method = RequestMethod.POST, produces = "text/html")
   public String create(@Valid ApplicationConfiguration applicationConfiguration, BindingResult bindingResult,
         Model uiModel, HttpServletRequest httpServletRequest, Principal principal) {
      if (bindingResult.hasErrors()) {
         populateEditForm(uiModel, applicationConfiguration);
         return "applicationconfigurations/create";
      }
      uiModel.asMap().clear();
      applicationConfiguration.setLastModificationUserId(principal.getName());
      applicationConfigurationService.saveApplicationConfiguration(applicationConfiguration);
      return "redirect:/applicationconfigurations/"
            + encodeUrlPathSegment(applicationConfiguration.getId().toString(), httpServletRequest);
   }

   @RequestMapping(value = "/create", produces = "text/html")
   public String createForm(Model uiModel) {
      populateEditForm(uiModel, new ApplicationConfiguration());
      return "applicationconfigurations/create";
   }

   @RequestMapping(value = "/{id}", produces = "text/html")
   public String show(@PathVariable("id") Long id, Model uiModel) {
      uiModel
            .addAttribute("applicationconfiguration", applicationConfigurationService.findApplicationConfiguration(id));
      uiModel.addAttribute("itemId", id);
      return "applicationconfigurations/show";
   }

   @RequestMapping(produces = "text/html")
   public String list(@RequestParam(value = "page", required = false) Integer page,
         @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
      if (page != null || size != null) {
         int sizeNo = size == null ? 10 : size.intValue();
         final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
         uiModel.addAttribute("applicationconfigurations",
               applicationConfigurationService.findApplicationConfigurationEntries(firstResult, sizeNo));
         float nrOfPages = (float) applicationConfigurationService.countAllApplicationConfigurations() / sizeNo;
         uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1
               : nrOfPages));
      }
      else {
         uiModel.addAttribute("applicationconfigurations",
               applicationConfigurationService.findAllApplicationConfigurations());
      }
      return "applicationconfigurations/list";
   }

   @RequestMapping(method = RequestMethod.PUT, produces = "text/html")
   public String update(@Valid ApplicationConfiguration applicationConfiguration, BindingResult bindingResult,
         Model uiModel, HttpServletRequest httpServletRequest, Principal principal) {
      if (bindingResult.hasErrors()) {
         populateEditForm(uiModel, applicationConfiguration);
         return "applicationconfigurations/update";
      }
      uiModel.asMap().clear();
      applicationConfiguration.setLastModificationUserId(principal.getName());
      applicationConfigurationService.updateApplicationConfiguration(applicationConfiguration);
      return "redirect:/applicationconfigurations/"
            + encodeUrlPathSegment(applicationConfiguration.getId().toString(), httpServletRequest);
   }

   @RequestMapping(value = "/update/{id}", produces = "text/html")
   public String updateForm(@PathVariable("id") Long id, Model uiModel) {
      populateEditForm(uiModel, applicationConfigurationService.findApplicationConfiguration(id));
      return "applicationconfigurations/update";
   }

   @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
   public String delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page,
         @RequestParam(value = "size", required = false) Integer size, Model uiModel, Principal principal) {
      ApplicationConfiguration applicationConfiguration = applicationConfigurationService
            .findApplicationConfiguration(id);
      applicationConfiguration.setLastModificationUserId(principal.getName());
      applicationConfigurationService.deleteApplicationConfiguration(applicationConfiguration);
      uiModel.asMap().clear();
      uiModel.addAttribute("page", (page == null) ? "1" : page.toString());
      uiModel.addAttribute("size", (size == null) ? "10" : size.toString());
      return "redirect:/applicationconfigurations";
   }

   void populateEditForm(Model uiModel, ApplicationConfiguration applicationConfiguration) {
      uiModel.addAttribute("applicationConfiguration", applicationConfiguration);
   }

   String encodeUrlPathSegment(String pathSegment, HttpServletRequest httpServletRequest) {
      String enc = httpServletRequest.getCharacterEncoding();
      if (enc == null) {
         enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
      }
      try {
         pathSegment = UriUtils.encodePathSegment(pathSegment, enc);
      }
      catch (UnsupportedEncodingException uee) {
      }
      return pathSegment;
   }
}
